package com.zbkj.crmeb.front.controller;

import com.common.CommonPage;
import com.crmeb.core.common.CommonResult;
import com.common.PageParamRequest;
import com.zbkj.crmeb.front.request.UserCollectAllRequest;
import com.zbkj.crmeb.front.request.UserCollectRequest;
import com.zbkj.crmeb.merchant.api.controller.ApiBaseController;
import com.zbkj.crmeb.store.model.StoreProduct;
import com.zbkj.crmeb.store.model.StoreProductRelation;
import com.zbkj.crmeb.store.request.StoreProductRelationSearchRequest;
import com.zbkj.crmeb.store.service.StoreProductRelationService;
import com.zbkj.crmeb.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 商品点赞和收藏表 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/front/collect")
@Api(tags = "用户 -- 点赞/收藏")

public class UserCollectController extends ApiBaseController {

    @Autowired
    private StoreProductRelationService storeProductRelationService;

    @Autowired
    private UserService userService;

    /**
     * 获取收藏产品
     * @param pageParamRequest 分页参数
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "获取收藏产品")
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public CommonResult<CommonPage<StoreProduct>> getList(@Validated String searchWord,@Validated PageParamRequest pageParamRequest, HttpServletRequest servletRequest){
        StoreProductRelationSearchRequest storeProductRelation = new StoreProductRelationSearchRequest();
        storeProductRelation.setUid(userService.getUserIdException());

        CommonPage<StoreProduct> storeProductCommonPage =
                CommonPage.restPage(storeProductRelationService.getList(searchWord, storeProductRelation, pageParamRequest, getMerId(servletRequest)));
        return CommonResult.success(storeProductCommonPage);
    }

    @ApiOperation(value = "失效商品列表")
    @RequestMapping(value = "/failure/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<StoreProduct>> getFailureList(@Validated PageParamRequest pageParamRequest, HttpServletRequest servletRequest){
        StoreProductRelationSearchRequest storeProductRelation = new StoreProductRelationSearchRequest();
        storeProductRelation.setUid(userService.getUserIdException());

        CommonPage<StoreProduct> storeProductCommonPage =
                CommonPage.restPage(storeProductRelationService.getFailureList(storeProductRelation, pageParamRequest, getMerId(servletRequest)));
        return CommonResult.success(storeProductCommonPage);
    }

    /**
     * 添加收藏产品
     * @param request StoreProductRelationRequest 新增参数
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "添加收藏产品")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated UserCollectRequest request){
        StoreProductRelation storeProductRelation = new StoreProductRelation();
        BeanUtils.copyProperties(request, storeProductRelation);
        Integer userId = userService.getUserIdException();
        storeProductRelation.setUid(userId);
        storeProductRelation.setType("collect");
        List<StoreProductRelation> list = storeProductRelationService.getLikeOrCollectByUser(userId,request.getProductId(),false);
        // 取消收藏
        if(list.size()>0){
            storeProductRelationService.delete(request);
            return CommonResult.success();
        }
        if(storeProductRelationService.save(storeProductRelation)){
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 添加收藏产品
     * @param request UserCollectAllRequest 新增参数
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "批量收藏")
    @RequestMapping(value = "/all", method = RequestMethod.POST)
    public CommonResult<String> all(@RequestBody @Validated UserCollectAllRequest request){
        if(storeProductRelationService.all(request)){
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 取消收藏产品
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "取消收藏产品")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public CommonResult<String> delete(@RequestBody @Validated UserCollectRequest request){
        request.setUid(userService.getUserIdException());
        request.setType("collect");
        if(storeProductRelationService.delete(request)){
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    @ApiOperation(value = "批量取消收藏产品")
    @RequestMapping(value = "/batch/del", method = RequestMethod.POST)
    public CommonResult<String> batchDelete(@RequestBody @Validated UserCollectRequest request){
        request.setUid(userService.getUserIdException());
        request.setType("collect");
        if(storeProductRelationService.batchDelete(request)){
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

}



